package com.ninthgroup.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ninthgroup.entity.Admin;
import com.ninthgroup.entity.News;
import com.ninthgroup.mapper.NewsMapper;
import com.ninthgroup.service.NewsService;
import com.ninthgroup.util.WebUtil;
import com.ninthgroup.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author ：Student王心
 * @date ：Created in 2022/10/14 15:23
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class NewsServiceImpl implements NewsService {
    @Autowired
    private NewsMapper newsMapper;


    /**
     * 根据新闻对象添加一条新闻
     * @param news
     * @return
     */
    @Override
    public Result insertNews(News news) {
         Admin admin = (Admin) WebUtil.getSession().getAttribute("admin");


        Map<String,Object> map = new HashMap<>();
        map.put("adminId",admin.getaId());
        news.setNewsStatus(0);
        newsMapper.insert(news);
        return new Result(200,"添加新闻成功",map);
    }

    /**
     * 查询所有新闻信息并携带分页、新闻类别信息
     * @param currentPage
     * @param pageSize
     * @param news
     * @return
     */
    @Override
    public Result findAllNews(Integer currentPage, Integer pageSize, News news) {
        PageHelper.startPage(currentPage,pageSize);
        List<News> newsList = newsMapper.selectAllNews(news);
        PageInfo<News> pageInfo = new PageInfo<>(newsList);
        return new Result(200,"查询当前发布新闻信息成功",pageInfo);
    }

    /**
     * 根据新闻对象获取审核状态(通过)
     * @param news
     * @return
     */
    @Override
    public Result updateNewsStatus(News news) {
        //状态为0，待审核，点击确定时状态修改为1，审核通过
            news.setNewsOuttime(new Date());
            news.setNewsStatus(1);
            newsMapper.updateByPrimaryKeyWithBLOBs(news);
            return new Result(200,"新闻审核成功");
    }

    /**
     * 根据新闻对象获取审核状态（未通过）
     * @param news
     * @return
     */
    @Override
    public Result updateAuditStatus(News news) {

        //状态为0，待审核，点击未通过
        //确定，修改为3
        //取消，返回未通过的页面
        news.setNewsStatus(3);
        newsMapper.updateByAduitStatus(news);
        return new Result(200,"审核未通过",news);
    }



    /**
     * 根据Id的查询拒绝理由
     * @param id
     * @return
     */
    @Override
    public Result findById(Integer id) {
        News news = newsMapper.selectByPrimaryKey(id);
        //每点击一次，点击量加1
        Integer newsClick = news.getNewsClick();
        news.setNewsClick(newsClick+1);
        newsMapper.updateByPrimaryKeySelective(news);
        return new Result(200,"查询拒绝理由成功",news);

    }

    /**
     * 根据新闻对象修改新闻
     * @param news
     * @return
     */
    @Override
    public Result updateNews(News news) {
        newsMapper.updateByPrimaryKeySelective(news);
        return new Result(200,"修改新闻成功");
    }

    /**
     * 根据当前页，页码，状态和置顶查询所有的新闻信息
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public Result findByUppid(Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<News> newsList = newsMapper.selectByUpPid();
        int j=0;
        for (int i = 0; i < newsList.size(); i++) {
            if(newsList.get(i).getNewsUppid()!=null){
                if (newsList.get(i).getNewsUppid()==6){
                    //将集合中的两列互换，i&j:用i去和j互换
                    Collections.swap(newsList,i,j);
                    j=j+1;
                }
            }

        }
        PageInfo<News> pageInfo = new PageInfo<News>(newsList);
        return new Result(200,"查询置顶信息成功",pageInfo);
    }

    /**
     * 客户端查询点击量进行排序
     * @return
     */
    @Override
    public Result findByNewsClick() {
        List<News> newsList = newsMapper.selectByNewsClick();
        for (int i = 0; i < 3; i++) {
            if (newsList.get(i).getKindId()==1){
                Collections.swap(newsList,0,i);
            }else if(newsList.get(i).getKindId()==2){
                Collections.swap(newsList,1,i);
            }else {
                Collections.swap(newsList,2,i);
            }
        }
        return new Result(200,"查询点击量成功",newsList);
    }

    /**
     * 客户端查询置顶的新闻
     * @return
     */
    @Override
    public Result findByNewsUppid() {
        List<News> newsList = newsMapper.selectByNewsUppid();
        return new Result(200,"查询置顶新闻成功",newsList);
    }


}